#!/bin/bash
#修改BuildConfig.plist文件，修改此时的时间和git提交对象,以及adhoc的版本号信息
scheme=$1
if  [ ! $scheme ] ;then
    echo "请输入scheme名称!"
    exit 0
fi
dir=`pwd`
plistFile="$dir/BuildConfig.plist"

addOrSetPlistString(){
    local file=$1
    local key=$2
    local val=$3
    /usr/libexec/PlistBuddy -c "Set :$key $val" $file || /usr/libexec/PlistBuddy -c "Add :$key string $val" $file
}

pushd ../
#更新版本
Version=`xcodebuild -showBuildSettings -scheme $scheme -json|jq -r '.[0].buildSettings.MARKETING_VERSION'`
addOrSetPlistString $plistFile VERSION $Version


#更新build号
Build=`xcodebuild -showBuildSettings -scheme $scheme -json|jq -r '.[0].buildSettings.CURRENT_PROJECT_VERSION'`
if [ ! $Build ] || [ $Build = 'null' ] ;then
    infoplist=`xcodebuild -showBuildSettings -scheme $scheme -json|jq -r '.[0].buildSettings.PRODUCT_SETTINGS_PATH'`
    Build=`/usr/libexec/PlistBuddy -c 'Print :CFBundleVersion' $infoplist`
fi
addOrSetPlistString $plistFile BUILD $Build

#更新时间
datetime=`date +%Y%m%d`
addOrSetPlistString $plistFile TIME $datetime

#更新git对象
gitcm=""
if  [ -d .git ] ;then
    gitcm=`git rev-parse --short HEAD`
    addOrSetPlistString $plistFile GIT_SHA $gitcm
fi

#更新文件名称
filename="$scheme-$datetime-$Version($Build)"
addOrSetPlistString $plistFile FILENAME $filename

echo "$filename"
popd

